//index.js
//获取应用实例
let rows = [
  {
    head: '../../static/image/head1.jpg',
    name: '网易火星网友',
    addr: '福建厦门市',
    phone: 'VIVO',
    date: '6小时前',
    topCounts: '1.8w',
    contents: '30万升降也经不起这么多软件来回测试'
  },
  {
    head: '../../static/image/head2.jpg',
    name: '网易火星网友',
    addr: '上海市',
    phone: 'VIVO',
    date: '6小时前',
    topCounts: 8564,
    contents: '30万升降也经不起这么多软件来回测试来回测试来回测试来回测试'
  },
  {
    head: '../../static/image/head3.jpg',
    name: '网易火星网友',
    addr: '北京市',
    phone: 'VIVO',
    date: '6小时前',
    topCounts: 1235,
    contents: '30万升降也经不起这么多软件来回测试也经不起这么多软件也经不起这么多软件'
  }
]
const app = getApp()
let imgs = [
  '../../static/image/swiper1.jpg',
  '../../static/image/swiper2.jpg',
  '../../static/image/swiper3.jpg',
  '../../static/image/list1.jpg',
  '../../static/image/list2.jpg',
  '../../static/image/list3.jpg',
  '../../static/image/list4.jpg',
  '../../static/image/list5.jpg'
]

Page({
  data: {
    menus_chosed: 1,
    menus: [
      { title: '推荐' },
      {
        title: '新闻', childs: [
          { title: '首页' },
          { title: '社会' },
          { title: '国内' },
          { title: '国际' },
          { title: '民生' },
          { title: '政治' },
          { title: '历史' }
        ]
      },
      {
        title: '娱乐',
        childs: [
          { title: '首页' },
          { title: '热闻' },
          { title: '八卦' },
          { title: '港台' },
          { title: '影视' }
        ]
      },
      {
        title: '体育', childs: [
          { title: '国内' },
          { title: '国际' },
          { title: '世界杯' },
          { title: 'NBA' }
        ]
      },
      { title: '图片' },
      { title: '财经' },
      { title: '汽车' },
      { title: '军事' },
    ],
    menus_child_chosed: 1,
    menus_clild: [
      { title: '首页' },
      { title: '社会' },
      { title: '国内' },
      { title: '国际' },
      { title: '民生' },
      { title: '政治' },
      { title: '历史' }
    ],
    imgUrls: [
      imgs[0],
      imgs[1],
      imgs[2]
    ],
    newList: [
      {
        title: '今日收盘：网游vr概念受挫 创业板尾盘跳水',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[2]],
        position: "left"
      },
      {
        title: '马斯克的“超级高铁”今日在美公开演示 一秒能跑300米',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[3]],
        position: "left"
      },
      {
        title: '学霸君与小米强强联手，全新miui 8系统搭载扫题功能',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[4]],
        position: "left"
      },
      {
        title: '全国人大会议日程公布:3月17日选举国家主席',
        date: '1小时',
        counts: 2345612,
        imgs: [imgs[1], imgs[0]],
        position: "bottom"
      },
      {
        title: '中国、韩国、日本智能手机普及率pk！吃惊',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[5]],
        position: "left"
      },
      {
        title: '印媒：富士康将在印度投资100亿美元，设厂生产iphone',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[6]],
        position: "left"
      },
      {
        title: '证监会严控定增 跨界虚拟产业被禁',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[7]],
        position: "left"
      },
      {
        title: '全国人大会议日程公布:3月17日选举国家主席',
        date: '1小时',
        counts: 2345612,
        imgs: [imgs[1]],
        position: "bottom"
      },
      {
        title: '今日收盘：网游vr概念受挫 创业板尾盘跳水',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[2]],
        position: "left"
      },
      {
        title: '马斯克的“超级高铁”今日在美公开演示 一秒能跑300米',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[3]],
        position: "left"
      },
      {
        title: '学霸君与小米强强联手，全新miui 8系统搭载扫题功能',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[4]],
        position: "left"
      },
      {
        title: '中国、韩国、日本智能手机普及率pk！吃惊',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[5]],
        position: "left"
      },
      {
        title: '全国人大会议日程公布:3月17日选举国家主席',
        date: '1小时',
        counts: 2345612,
        imgs: [imgs[1], imgs[0], imgs[6]],
        position: "bottom"
      },
      {
        title: '印媒：富士康将在印度投资100亿美元，设厂生产iphone',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[6]],
        position: "left"
      },
      {
        title: '证监会严控定增 跨界虚拟产业被禁',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[7]],
        position: "left"
      },
    ],
    headTitle:"",
    contentTitle:"习近平致电祝贺非洲联盟第31届首脑会议召开",
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: rows,
    scrollTop:0,
    showLoading: false
  },

  chooseMenu(e) {
    let key = e.currentTarget.dataset['key'];
    let childs = e.currentTarget.dataset['childs'];
    this.setData({
      menus_chosed: key
    })
    this.setData({
      menus_clild: childs || []
    })
  },
  chooseChildMenu(e) {
    let key = e.currentTarget.dataset['key'];
    this.setData({
      menus_child_chosed: key
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onPageScroll() {
    let that = this;
    wx.createSelectorQuery().selectViewport().scrollOffset(function (res) {
      let _change = false;
      let _title= "";
      if (res.scrollTop >= 66 && that.data.headTitle === ''){
        _title = that.data.contentTitle;
        _change = true;
      }
      if (res.scrollTop < 66 && that.data.headTitle !== ''){
        _change = true;
      }
      if(_change){
        wx.setNavigationBarTitle({
          title: _title
        })
        that.setData({
          headTitle: _title
        })
      }
    }).exec()
  }
})
